//
//  IntroductionView.m
//  xpkc
//
//  Created by wanmeizty on 14/12/26.
//  Copyright (c) 2014年 wanmeizty. All rights reserved.
//
/*****************************
 *引导页视图
 *视差效果
 *多元素滚动效果（主视图附加，scrollview同级）
 *
 *
 *
 *
 */
#import "IntroductionView.h"

@implementation IntroductionView{
    int introductionNum;//引导数量
    UIScrollView *introductionView;//引导滚动页
    UIView *bgView;//背景
    UIView *ornamentView;//装饰
    UIView *pointView;//点
    
    int indexPage;//当前页
}
- (id)initWithFrame:(CGRect)frame{
    self  = [super initWithFrame:frame];
    if (self) {
        //
        
        
    }
    return self;
}

-(void)setIsNotIntroduction:(BOOL)isNotIntroduction{
    
    [self creatIntroductionView:isNotIntroduction];
}

/**
 *  创建视图
 */
- (void)creatIntroductionView:(BOOL)isNotIntroduction{
    self.backgroundColor = [UIColor whiteColor];
    introductionNum = 4;
    //背景
    bgView = [[UIView alloc]initWithFrame:CGRectMake(-100, 0, self.frame.size.width*introductionNum-2, self.frame.size.height)];
    //TODO:背景的定制
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, bgView.frame.size.width, self.frame.size.height)];
    image.image = [UIImage imageNamed:@"cloudbg.jpeg"];
    [bgView addSubview:image];
    [self addSubview:bgView];
    
    //装饰
    ornamentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width*introductionNum-1, self.frame.size.height)];
    //TODO:装饰定制
    ornamentView.backgroundColor = [UIColor clearColor];
    UIImage *flyImg = [UIImage imageNamed:@"flay.png"];
    UIImageView *flyView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 320)];
    flyView.image = flyImg;
    flyView.tag = 1;
    flyView.hidden = YES;
    [ornamentView addSubview:flyView];
    
    [self addSubview:ornamentView];
    
    //引导页
    introductionView = [[UIScrollView alloc]initWithFrame:self.bounds];
    introductionView.contentSize = CGSizeMake(introductionView.frame.size.width*introductionNum, introductionView.frame.size.height);
    introductionView.delegate = self;
    introductionView.backgroundColor = [UIColor clearColor];
    introductionView.pagingEnabled = YES;
    introductionView.alwaysBounceVertical = NO;
    introductionView.showsHorizontalScrollIndicator = NO;
    //FIXME:添加测试用例
    
    self.mainBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.mainBtn.frame = CGRectMake(0.25*SCREEN_WIDTH, self.frame.size.height*7/8 - 34 , SCREEN_WIDTH * 0.5, 45);
    //    self.mainBtn.backgroundColor = [UIColor colorWithRed:0.3 green:0.6 blue:0.9 alpha:0.3];
    //    [self.mainBtn setTitle:@"欢迎进入" forState:UIControlStateNormal];
    [self.mainBtn addTarget:self action:@selector(outIntroduction) forControlEvents:UIControlEventTouchUpInside];
    self.mainBtn.layer.cornerRadius = 5;
    self.mainBtn.layer.masksToBounds = YES;
    NSArray *imgarray ;
    if (!isNotIntroduction) {
        imgarray = @[@"引导页1.png",@"引导页2.png",@"引导页3.png",@"引导页4_按钮参考.png"];
        
        
    }else{
        self.mainBtn.backgroundColor = [UIColor colorWithRed:0.3 green:0.6 blue:0.9 alpha:0.3];
        [self.mainBtn setTitle:@"欢迎进入" forState:UIControlStateNormal];
        imgarray = @[@"引导页1.png",@"引导页2.png",@"引导页3.png",@"引导页4.png"];
    }
    
    for (int i = 0; i<introductionNum; i++) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(i*SCREEN_WIDTH, 0, SCREEN_WIDTH, self.frame.size.height)];
        view.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:0.1];
        
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        
        img.image = [UIImage imageNamed:[imgarray objectAtIndex:i]];
        
        [view addSubview:img];
        if (i == introductionNum-1) {
            [view addSubview:self.mainBtn];
            
        }
        
        [introductionView addSubview:view];
        
    }
    
    [self addSubview:introductionView];
    
    //点
    pointView = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height*7/8 - 31, SCREEN_WIDTH, 50)];
    
    
    for (int i = 0; i<introductionNum; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        float sx = SCREEN_WIDTH/2-introductionNum/2.0*20;
        btn.frame = CGRectMake(sx+20*i, 10, 10, 10);
        btn.tag = i;
        btn.backgroundColor = [UIColor whiteColor];
        btn.layer.cornerRadius = 5;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            btn.transform = CGAffineTransformMakeScale(1.3, 1.3);
        }
        [pointView addSubview:btn];
    }
    
    [self addSubview:pointView];
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    indexPage = (int)scrollView.contentOffset.x/(int)SCREEN_WIDTH;
    bgView.frame = CGRectMake(-scrollView.contentOffset.x/3-100, 0, bgView.frame.size.width, self.frame.size.height);
    ornamentView.frame = CGRectMake(indexPage*(bgView.frame.size.width-SCREEN_WIDTH)/introductionNum, 0, SCREEN_WIDTH, self.frame.size.height);
    for (UIButton *btn in pointView.subviews) {
        
        if (indexPage == pointView.subviews.count - 1 && !_isNotIntroduction) {
            
            pointView.hidden = YES;
            btn.hidden = YES;
        }else{
            pointView.hidden = NO;
            btn.hidden = NO;
        }
        
        
        if (btn.tag == indexPage) {
            //
            [UIView animateWithDuration:0.3 animations:^{
                btn.transform = CGAffineTransformMakeScale(1.3, 1.3);
            }];
        }else{
            [UIView animateWithDuration:0.3 animations:^{
                btn.transform = CGAffineTransformMakeScale(1, 1);
            }];
        }
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (indexPage == 1) {
        //
        for (UIImageView *img in ornamentView.subviews) {
            if (img.tag == 1) {
                //初始位置
                img.center = CGPointMake(-400, self.frame.size.height*2/3);
                img.hidden = NO;
                
                [UIView animateWithDuration:1.9 animations:^{
                    //
                    img.center = CGPointMake(self.frame.size.height*2/3, -400);
                } completion:^(BOOL finished) {
                    img.hidden = YES;
                }];
            }
        }
    }
}

- (void)btnClick:(UIButton *)btn{
    [UIView animateWithDuration:0.3 animations:^{
        introductionView.contentOffset = CGPointMake(btn.tag*SCREEN_WIDTH, 0);
    }];
    
}
- (void)outIntroduction{
    [self removeFromSuperview];
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
